package com.wordLearning.word.domain.service.impl;

import com.wordLearning.word.common.entity.PageResult;
import com.wordLearning.word.domain.convert.FavoriteBOConverter;
import com.wordLearning.word.domain.convert.UnmasteredBOConverter;
import com.wordLearning.word.domain.convert.WordInfoConverter;
import com.wordLearning.word.domain.entity.FavoriteWordBO;
import com.wordLearning.word.domain.entity.PlanBO;
import com.wordLearning.word.domain.entity.UnmasteredWordBO;
import com.wordLearning.word.domain.entity.WordBO;
import com.wordLearning.word.domain.service.LearningdomainService;
import com.wordLearning.word.infra.basic.entity.FavoriteWord;
import com.wordLearning.word.infra.basic.entity.LearningRecord;
import com.wordLearning.word.infra.basic.entity.UnmasteredWord;
import com.wordLearning.word.infra.basic.entity.Word;
import com.wordLearning.word.infra.basic.service.FavoriteWordService;
import com.wordLearning.word.infra.basic.service.LearningRecordService;
import com.wordLearning.word.infra.basic.service.UnmasteredWordService;
import com.wordLearning.word.infra.basic.service.WordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class LearningdomainServiceImpl implements LearningdomainService {

    @Resource
    private WordService wordService;

    @Resource
    private LearningRecordService learningRecordService;

    @Resource
    private UnmasteredWordService unmasteredWordService;

    @Resource
    private FavoriteWordService favoriteWordService;

    @Override
    public List<PageResult<WordBO>> setLearningPlan(PlanBO planBO) {
        List<PageResult<WordBO>> list = new ArrayList<>();
        int day = planBO.getDay();
        int amount = planBO.getAmount();
        List<Word> wordToAddList = new ArrayList<>();
        for(int i = 1;i <= day;i++){
            List<Integer> learnedList = learningRecordService.selectWordIds();
            int start = (i - 1) * amount;
            Word word = WordInfoConverter.INSTANCE.convertBoToEntity(new WordBO());
            int count = wordService.countByCondition(word);
            if(count == 0){
                return null;
            }
            List<Word> wordList = wordService.setLearningPlan(word,start,amount,learnedList);
            for(Word toAdd : wordList){
                wordToAddList.add(toAdd);
            }
            List<WordBO> boList = WordInfoConverter.INSTANCE.convertEntityListToBOList(wordList);
            PageResult<WordBO> pageResult = new PageResult<>();
            pageResult.setResult(boList);
            pageResult.setTotal(count);
            list.add(pageResult);
        }
        for(Word wordToAdd : wordToAddList){
            LearningRecord learningRecord = new LearningRecord();
            learningRecord.setUserId(1);
            learningRecord.setWordId(wordToAdd.getWordId());
            learningRecord.setStatus(0);
            add(learningRecord);
        }
        return list;
    }

    public void add(LearningRecord learningRecord) {
        learningRecordService.insert(learningRecord);
    }

    @Override
    public void forget(UnmasteredWordBO unmasteredWordBO) {
        UnmasteredWord unmasteredWord = UnmasteredBOConverter.INSTANCE.convertUnmasteredWordBOToEntity(unmasteredWordBO);
        unmasteredWordService.insert(unmasteredWord);
    }

    @Override
    public void favorite(FavoriteWordBO favoriteWordBO) {
        FavoriteWord favoriteWord = FavoriteBOConverter.INSTANCE.convertFavoriteBOToEntity(favoriteWordBO);
        favoriteWordService.insert(favoriteWord);
    }

    @Override
    public int remember(UnmasteredWordBO unmasteredWordBO) {
        UnmasteredWord unmasteredWord = UnmasteredBOConverter.INSTANCE.convertUnmasteredWordBOToEntity(unmasteredWordBO);
        return unmasteredWordService.deleteByCondition(unmasteredWord.getWordId());
    }

    @Override
    public int cancelFavorite(FavoriteWordBO favoriteWordBO) {
        FavoriteWord favoriteWord = FavoriteBOConverter.INSTANCE.convertFavoriteBOToEntity(favoriteWordBO);
        return favoriteWordService.deleteByCondition(favoriteWord.getWordId());
    }
}
